package com.example.administrator.capital.mine;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.capital.R;
import com.example.administrator.capital.adapter.CommentAdapter;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.eventbus.EventCardBean;
import com.example.administrator.capital.model.bean.BankList;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.ToastUtil;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import eventbusactivityscope.EventBusActivityScope;
import layout.LoadingLayout;
import recycle.BaseViewHolder;

/**
 * A simple {@link Fragment} subclass.
 * 银行卡列表
 */
public class CardFragment extends BaseBackFragment {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    Unbinder unbinder;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.loading)
    LoadingLayout loading;
    private MinePresenter minePresenter;
    private CommentAdapter<BankList.DataBean.BankBean> commentAdapter;
    private TextView mBack;
    public CardFragment() {
        // Required empty public constructor
    }
    public static CardFragment newInstance() {
        Bundle args = new Bundle();
        CardFragment fragment = new CardFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_card, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mBack = view.findViewById(R.id.app_title_back);
        minePresenter = new MinePresenter(null);
        swipeLayout.setColorSchemeResources(R.color.font_blue);
        commentAdapter = new CommentAdapter<BankList.DataBean.BankBean>(R.layout.bank_item_list, null) {
            @Override
            public void setViewData(BaseViewHolder holder, BankList.DataBean.BankBean item, int position) {
                holder.setText(R.id.bankname, item.getBankname()).setText(R.id.bankaccount, item.getBankaccount());
            }
            @Override
            public void setEvent(BaseViewHolder holder, final BankList.DataBean.BankBean item, int position) {
                holder.setOnClickListener(R.id.layout, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        EventBusActivityScope.getDefault(getActivity()).post(new EventCardBean(item.getBankid(),item.getBankname(),item.getBankaccount()));
                        _mActivity.onBackPressed();
                    }
                });

            }
        };
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration div = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        div.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.custom_divider));
        recycleView.addItemDecoration(div);
        recycleView.setAdapter(commentAdapter);
    }
    @Override
    protected void setListener() {
        super.setListener();
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();
            }
        });
    }
    @Override
    protected void setData() {
        super.setData();
        getData();
    }
    private void getData() {
        minePresenter.banklist(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }
            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }
            @Override
            public void onSuccess(Response<String> data) {
                BankList bean = JsonUtils.parse(data.body(), BankList.class);
                commentAdapter.setNewData(bean.getData().getBank());
                loading.showContent();
            }
            @Override
            public void onFail(Response<String> error) {
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
